<template>
  <div class="">
    <div class="bg_fff p_10 listbody">
      <div class="flexrowbetween flexwrapper" style="align-items: flex-start">
        <div class="flex">
          <div class="iconbox flexcenter commhover mr_10" @click="resetQuery">
            <i class="el-icon-refresh f-20" :class="{ rotate: isrefresh }"></i>
          </div>
          <el-button plain type="primary" size="mini" @click="handleAdd">添加</el-button>
        </div>
        <div>
          <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            label-width="0"
          >
            <el-form-item prop="keyword">
              <el-input
                class="searchinput"
                v-model="queryParams.keyword"
                placeholder="请输入名称/ID"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <!-- <el-form-item>
              <el-dropdown trigger="click" @command="handleCommand">
                <el-button size="mini" class="nol_border plr_9">
                  <svg-icon class-name="f-14" icon-class="export" />
                  <i class="el-icon-caret-bottom el-icon--right"></i>
                </el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item
                    v-for="(item, index) in exportlist"
                    :key="index"
                    :command="item.id"
                    >{{ item.name }}</el-dropdown-item
                  >
                </el-dropdown-menu>
              </el-dropdown>
            </el-form-item> -->
            <el-form-item>
              <el-button class="f-14"
                icon="el-icon-search"
                size="mini"
                @click="handleQuery"
                ></el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        ref="tablelayout"
        class="commtable"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column align="center"
          label="序号"
          prop="id"
          width="80"
        >
          <template slot-scope="scope">
            <span>{{(queryParams.page - 1) * queryParams.num + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center"
          label="ID"
          prop="id"
          width="80"
        ></el-table-column>
        <el-table-column align="center"
          label="模块类型"
          prop="type_name"
          width="120"
        ></el-table-column>
        <el-table-column align="center"
          label="名称"
          prop="name"
        ></el-table-column>
        <el-table-column align="center"
          label="链接"
          prop="id"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.url||'-' }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center"
          label="备注"
          prop="id"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.msg||'-' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否展示"
          align="center"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#eee"
              :inactive-value="0"
              :active-value="1"
              @change="changeStatus($event,scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column
            prop="created_at"
            label="操作"
            width="180"
            fixed="right"
            align="center"
        >
          <template slot-scope="scope">
            <div class="flex btnflex">
                <div class="commhover btn_green c_fff mr_10"
                  @click="handleUpdate(scope.row)">
                  编辑
                </div>
                <div class="commhover btn_yellow c_fff mr_10"
                  @click="handleDelete(scope.row)">
                  删除
                </div>
                <div v-if="scope.row.is_round==1" class="commhover btn_blue c_fff mr_10" @click="handleOpenCc(scope.row)">
                  场次
                </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.num"
        @pagination="getList"
      />
    </div>
   
    <el-dialog :title="title"
      :visible.sync="open"
      width="580px"
      class="dialogheaderbg"
      :close-on-click-modal='false'
      append-to-body
      :before-close="handleCloseAdd"
      >
      <add-model v-if="open" :rowid='rowid' @close='handleCloseAdd' />
    </el-dialog>

    <el-dialog title="场次设置"
      :visible.sync="ccopen"
      width="900px"
      class="dialogheaderbg"
      :before-close="handleCloseCc"
      :close-on-click-modal='false'
      append-to-body>
      <field-in v-if="ccopen" :rowid='rowid' @close='handleCloseCc' />
    </el-dialog>
  </div>
</template>

<script>
import { mapState } from "vuex";
import AddModel from './AddModel.vue'
import FieldIn from './FieldIn.vue'
import { moduleIndex, moduleStatusClose, moduleStatusOpen, moduleDel } from '@/api/sethome';
import { getClassIndex, classAdd, classEdit, classLevelEdit, classStatusEdit, classDel} from '@/api/template'
export default {
  data() {
    return {
      queryParams: {
        page: 1,
        num: 10,
        keyword: undefined,
      },
      total: 0,
      loading: false,
      tableData: [],
      isrefresh: false,
      multiple: true,
      title: "",
      // 是否显示弹出层
      open: false,
      ccopen:false,
      rowid:''
    };
  },

  components:{
    AddModel,
    FieldIn
  },

  created() {
    this.getList()
  },

  methods: {
    getList() {
      this.loading = true;
      let params = {
        page: this.queryParams.page,
        num: this.queryParams.num
      }
      if(this.queryParams.keyword){
        params.keyword = this.queryParams.keyword
      }
      moduleIndex(params).then((response) => {
        this.tableData = response.data.list;
        this.total = response.data.count;
        this.loading = false;
        this.isrefresh = false;
      });
    },

    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },

    resetQuery() {
      this.isrefresh = true;
      this.resetForm("queryForm");
      this.handleQuery();
    },

    handleSelectionChange(selection) {
      this.rowids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },

    changeStatus(val,row){
      console.log('val:', val)
      if(val==0){
        this.$modal.confirm('是否隐藏分类').then(() => {
          return moduleStatusClose({
            id:row.id
          });
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess('隐藏成功');
        }).catch(() => {
          row.status = val ? 0 : 1
        });
      }else{
        this.$modal.confirm('是否显示分类').then(() => {
          return moduleStatusOpen({
            id:row.id
          });
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess('显示成功');
        }).catch(() => {
          row.status = val ? 0 : 1
        });
      }
    },

    reset(){
      this.form = {
        id: undefined,
        name: undefined,
        level:undefined,
        status: 1,
      };
      this.resetForm("form");
    },

    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.type = 1;
      this.rowid = '';
      this.open = true;
      this.title = "添加";
    },

    handleCloseAdd(type){
      if(type==1) this.getList()
      this.open = false
    },

    
    handleCommand(command) {
      console.log(command);
    },


    /** 修改按钮操作 */
    handleUpdate(row) {
      this.rowid = row.id
      this.open = true;
      this.title = "修改";
    },

    // 排序
    handleSort(row) {
      this.reset();
      this.type = 3;
      this.form.id = row.id;
      this.form.level = row.level
      this.open = true;
      this.title = "修改";
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.type == 2) {
            classEdit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else if (this.type == 1)  {
            classAdd(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }else if (this.type == 3){
            classLevelEdit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const infoIds = row.id;
      this.$modal.confirm('是否确认删除编号为"' + infoIds + '"的数据项？').then(() => {
        return moduleDel({id:infoIds});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    
    handleOpenCc(row){
      this.rowid = row.id
      this.ccopen = true
    },

    handleCloseCc(){
      this.ccopen = false
    }
  },
};
</script>

<style lang="scss" scoped>
.iconbox {
  width: 32px;
  height: 32px;
  border: 1px solid #eee;
  border-radius: 2px;
}
</style>
